package com.shop.shoporder.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.shop.shoporder.common.HttpRequest;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.entity.HttpRequestConfig;
import com.shop.shoporder.entity.order.CartList;
import com.shop.shoporder.entity.order.Carts;
import com.shop.shoporder.entity.order.Order;
import com.shop.shoporder.entity.order.OrderReturns;
import com.shop.shoporder.mapper.order.OrderReturnsMapper;
import com.shop.shoporder.service.CartService;
import com.shop.shoporder.service.OrderReturnsService;
import com.shop.shoporder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service("orderReturnsService")
public class OrderReturnsServiceImpl implements OrderReturnsService {
    @Autowired
    OrderReturnsMapper orderReturnsMapper;
    @Autowired
    OrderService orderService;
    @Autowired
    CartService cartService;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return orderReturnsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(OrderReturns record) {
        return orderReturnsMapper.insert(record);
    }

    @Override
    public int insertSelective(OrderReturns record) {
        return orderReturnsMapper.insertSelective(record);
    }

    @Override
    public OrderReturns selectByPrimaryKey(Integer id) {
        return orderReturnsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(OrderReturns record) {
        return orderReturnsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(OrderReturns record) {
        return orderReturnsMapper.updateByPrimaryKey(record);
    }

    @Override
    public OrderReturns selectByOrderNo(String orderNo) {
        return orderReturnsMapper.selectByOrderNo(orderNo);
    }

    @Transactional
    @Override
    public Response applyReturn(Map<String, String> map, HttpServletRequest request) throws Exception {

        int userNoId=0;
        String orderNo="";
        String returnReason="";
        OrderReturns orderReturns=new OrderReturns();
        if (map != null) {
            userNoId = InterfaceVer.VerNumber(map.get("userNoId"));
            orderNo = InterfaceVer.VerStr(map.get("orderNo"));
            returnReason = InterfaceVer.VerStr(map.get("returnReason"));
            OrderReturns orderReturns1=orderReturnsMapper.selectByOrderNo(orderNo);
            if(orderReturns1!=null){
                return new Response(MessageConstants.MSG_ADD_FAILED, 0, 0, "不允许重复申请!", request);
            }
            List<CartList> lists=cartService.selectCartListByOrderNo(userNoId,orderNo);
            if(lists!=null&&lists.size()>0){
                for (CartList cartList:lists) {
                    for (Carts carts : cartList.getCartsList()) {
                        int remark3=carts.getRemark3()==null?0:carts.getRemark3();
                        if(remark3==1){
                            return new Response(MessageConstants.MSG_ADD_FAILED, 0, 0, "申请失败,请联系客服", request);
                        }
                    }
                }
            }else {
                throw new RuntimeException();
            }
            orderReturns.setOrderNo(orderNo);
            orderReturns.setReturnReason(returnReason);
            orderReturns.setUsernoId(userNoId);
            int res=insertSelective(orderReturns);
            if(res>0){
                Order order=orderService.selectByOrderNo(orderNo);
                if(order!=null){
                    order.setOrderStatus("704");
                    int res1=orderService.updateByPrimaryKeySelective(order);
                    if(res1>0){
                        return new Response(MessageConstants.MSG_SUCCESS, 1, 1, "申请成功!", request);
                    }else {
                        throw new RuntimeException();
                    }
                }else {
                    throw new RuntimeException();
                }
            }else {
                return new Response(MessageConstants.MSG_ADD_FAILED, 0, 0, "申请失败code:401!", request);
            }
        }else {
            return new Response(MessageConstants.MSG_ADD_FAILED, 0, 0, "申请失败code:402!", request);
        }
    }

    @Override
    public JSONObject reFund(Order order) {
        double inter=order.getInter()==null?0:order.getInter();
        double costInter=order.getCostInter()==null?0:order.getCostInter();
        double cashCoupon=order.getCashCoupon()==null?0:order.getCashCoupon();
        double coupon=order.getCoupon()==null?0:order.getCoupon();
        double freight=order.getFreight()==null?0:order.getFreight();
        double cash=order.getCash()==null?0:order.getCash();
        int userNoId=order.getUsernoId()==null?0:order.getUsernoId();
        String orderNo=order.getOrderNo()==null?"":order.getOrderNo();
        inter=inter+cash;
        String url = HttpRequestConfig.finaceServerUrl+"/wap/refund";
        String parm = "inter=" + inter+"&costInter="+costInter+"&cashCoupon="+cashCoupon+"&coupon="+coupon+"&userNoId="+userNoId+"&orderNo="+orderNo;
        String result = HttpRequest.sendGet(url, parm);
        JSONObject str = JSONObject.parseObject(result);
        return str;
    }

    @Override
    public JSONObject weChatReFund(String orderNo, String allCash, String reFondCash) {
        String url = HttpRequestConfig.webServerUrl+"/wap/refund";
        String parm = "orderNo=" + orderNo+"&allCash="+allCash+"&reFondCash="+reFondCash;
        String result = HttpRequest.sendGet(url, parm);
        JSONObject str = JSONObject.parseObject(result);
        String code = str.get("code").toString();
        if ("000000".equals(code)) {
            JSONObject strData = JSONObject.parseObject(str.get("data").toString());
            return strData;
        } else {
            return null;
        }
    }
}
